/*
 * G4Sipm.cc
 *
 * @created Apr 22, 2013
 * @author Tim Niggemann, III Phys. Inst. A, RWTH Aachen University
 * @copyright GNU General Public License v3.0
 */

#include "G4Sipm.hh"

#include <G4Box.hh>
#include <G4SDManager.hh>
#include <G4VisAttributes.hh>
#include <G4DigiManager.hh>

#include "hit/G4SipmSensitiveDetector.hh"
#include "digi/G4SipmDigitizer.hh"
#include "digi/G4SipmVoltageTraceDigitizer.hh"

G4SipmId G4Sipm::LAST_ID = 0;

G4Sipm::G4Sipm(G4SipmModel *_model) : Placeable(), model(_model)
{
	setName("g4sipm");
	// Nifty counter.
	// 递增标识符，确保每个SiPM都有一个唯一的标识符
	id = LAST_ID++;
}

G4Sipm::~G4Sipm()
{
	// Deactivate sensitive detector.
	// 停用所用灵敏探测器
	if (G4SDManager::GetSDMpointer()->FindSensitiveDetector(getSensitiveDetectorName(), false))
	{
		G4SDManager::GetSDMpointer()->Activate(getSensitiveDetectorName(), false);
	}
	// Fetch digitizer modules and set pointer to the SiPM properly to NULL.
	// This also prevents the digitizers to take action.
	// 获取数字化模块并将其指针设置为空
	G4SipmDigitizer *digitizer = reinterpret_cast<G4SipmDigitizer *>(G4DigiManager::GetDMpointer()->FindDigitizerModule(
		getDigitizerName()));
	if (digitizer)
	{
		digitizer->setSipm(NULL);
	}
	G4SipmVoltageTraceDigitizer *vtDigitizer =
		reinterpret_cast<G4SipmVoltageTraceDigitizer *>(G4DigiManager::GetDMpointer()->FindDigitizerModule(
			getVoltageTraceDigitizerName()));
	if (vtDigitizer)
	{
		vtDigitizer->setSipm(NULL);
	}
}
// 构建SiPM逻辑体，并设置相关灵敏探测器和数字化模块
G4LogicalVolume *G4Sipm::build()
{
	// Build SiPM solid and logical volume.
	G4VSolid *solid = new G4Box(getName() + "Solid", model->getPitch() / 2., model->getPitch() / 2.,
								model->getThickness() / 2.);
	G4SipmSensitiveDetector *sd = (G4SipmSensitiveDetector *)G4SDManager::GetSDMpointer()->FindSensitiveDetector(
		getSensitiveDetectorName(), false);
	// 若没有设置灵敏探测器则新建一个
	if (!sd)
	{
		sd = new G4SipmSensitiveDetector(this);
	}
	// 将SiPM设置为灵敏探测器
	G4LogicalVolume *logicalVolume = new G4LogicalVolume(solid, model->getMaterial(), getName() + "Lv", NULL, sd);
	logicalVolume->SetVisAttributes(new G4VisAttributes(G4Color::Green()));
	// Create digitizer modules.
	// 创建数字化模块
	if (!G4DigiManager::GetDMpointer()->FindDigitizerModule(getDigitizerName()))
	{
		new G4SipmDigitizer(this);
	}
	if (!G4DigiManager::GetDMpointer()->FindDigitizerModule(getVoltageTraceDigitizerName()))
	{
		new G4SipmVoltageTraceDigitizer(this);
	}
	//
	return logicalVolume;
}
// 获取SiPM的标识符
G4SipmId G4Sipm::getId() const
{
	return id;
}
// 获取SiPM的模型
G4SipmModel *G4Sipm::getModel() const
{
	return model;
}
// 获取灵敏探测器名称，"g4sipmSd-"+SiPM标识符
G4String G4Sipm::getSensitiveDetectorName() const
{
	std::ostringstream o;
	o << "g4sipmSd-" << id;
	return o.str();
}
// 获取命中集合名称，"g4sipmHits-"+SiPM标识符
G4String G4Sipm::getHitCollectionName() const
{
	std::ostringstream o;
	o << "g4sipmHits-" << id;
	return o.str();
}
// 获取数字化集合名称，"g4sipmDigis-"+SiPM标识符
G4String G4Sipm::getDigiCollectionName() const
{
	std::ostringstream o;
	o << "g4sipmDigis-" << id;
	return o.str();
}
// 获取电压跟踪数字化集合名称，"g4sipmVoltageTraceDigis-"+SiPM标识符
G4String G4Sipm::getVoltageTraceDigiCollectionName() const
{
	std::ostringstream o;
	o << "g4sipmVoltageTraceDigis-" << id;
	return o.str();
}
// 获取数字化器名称，"g4sipmDigitizer-"+SiPM标识符
G4String G4Sipm::getDigitizerName() const
{
	std::ostringstream o;
	o << "g4sipmDigitizer-" << id;
	return o.str();
}
// 获取电压跟踪数字化器名称，"g4sipmVoltageTraceDigitizer-"+SiPM标识符
G4String G4Sipm::getVoltageTraceDigitizerName() const
{
	std::ostringstream o;
	o << "g4sipmVoltageTraceDigitizer-" << id;
	return o.str();
}
